using System;
using System.Collections.Generic;
using System.Text;
using SP.Parameter;

namespace SP.HwDeferredRS
{
    /// <summary>
    /// A Distortion Fusion
    /// </summary>
    public class DistortionFusion : AbstractFusion
    {
        public DistortionFusion()
        {
            // param sys_Refract
            AddParameter(
                new Texture2dParamter("sys_Refract", "sys_RefractSamp")
                .SetSRGBTexture(false)
                .SetEditable(false)
                .SetUserData("fragment")
                );

            AddParameter(
                new Vec2Parameter("distortionScale")
                .SetX(0.05f).SetY(0.05f)
                .SetDesc("[0.0 - 0.2], the larger the value, the stronger the distortion effect\nRemember to use Render Queue Id >= 60!!")
                );
        }

        public static string NAME = "Distortion";

        public override string Name
        {
            get { return NAME; }
        }

        public override string Desc
        {
            get { return ""; }
        }

        public override Stage Stage
        {
            get { return Stage.AlbedoGloss; }
        }

        public override int UVChannelCount
        {
            get { return 0; }
        }

        public override void Code(StringBuilder b, string platform)
        {
            AddLine(b, "vec4 sys_Refract = tex2D(sys_RefractSamp, cReprojUV + pipeline.Normal.xy * distortionScale.xy);");
            AddLine(b, "float ndotv = saturate(dot(pipeline.Normal, -pipeline.ViewDir));");
            AddLine(b, "pipeline.Albedo.rgb = lerp(sys_Refract.rgb, pipeline.Albedo.rgb, ndotv * pipeline.Opacity);");
            //AddLine(b, "pipeline.Albedo.rgb = sys_Refract.rgb;");
        }
    }
}
